﻿using System.Windows.Controls;
using GalaSoft.MvvmLight.Messaging;

namespace Songhay.Silverlight.BiggestBox.IndexPart.Views
{
    using Songhay.Mvvm.Models;

    /// <summary>
    /// Window for Packed XAML
    /// </summary>
    public partial class PackedXamlChildWindow : ChildWindow
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="PackedXamlChildWindow"/> class.
        /// </summary>
        public PackedXamlChildWindow()
        {
            InitializeComponent();

            #region eventing:

            this.Closed += (s, args) =>
            {
                if(this._isClosing) return;
                this._isClosing = true;

                var message = new LightMessage<ChildWindow>(this)
                {
                    DisplayText = (string)this.Title,
                    Source = LightMessageSource.ViewEventClosing,
                    UniqueId = "PackedXamlChildWindow"
                };
                Messenger.Default.Send(message);
            };

            #endregion

            #region messaging:

            Messenger.Default.Register<LightNotificationMessageAction<ChildWindow>>(this,
            message =>
            {
                message.Execute(this);
            });

            #endregion
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="PackedXamlChildWindow"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        /// <param name="xaml">The xaml.</param>
        public PackedXamlChildWindow(UserControl control, string xaml)
            : this()
        {
            InitializeComponent();

            this.SampleTabControl.Loaded += (s, args) =>
            {
                this.Title = (string)control.Tag;
                this.SamplePresenter.Content = control;
                this.SampleXamlSource.Text = xaml;
            };
        }

        bool _isClosing;
    }
}

